package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.item.interfaces.pojo.Brand;
import com.leyou.item.interfaces.pojo.TbCategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.mapper.TbCategoryBrandMapper;
import com.leyou.item.service.BrandService;
import com.leyoucommon.pojo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.List;

/**
 * 品牌的Brandservice的实现类
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Service
@Transactional
public class BrandServiceImpl implements BrandService {

    /**
     * 注入TbCategoryBrandMapper
     */
    @Autowired
    private TbCategoryBrandMapper tbCategoryBrandMapper;

    /**
     * 注入BrandMapper
     */
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 新增品牌
     * @param brand 品牌参数
     * @param cids 分类的id
     * @return ResponseEntity<Void>
     */
    @Override
    public ResponseEntity<Void> saveBrand(Brand brand, List<Long> cids) {
        // 先add品牌数据
        this.brandMapper.insertSelective(brand);
        // 再新增brand和Category中间表
        TbCategoryBrand tbCategoryBrand = null;
        for (int i = 0; i < cids.size(); i++) {
            tbCategoryBrand = new TbCategoryBrand();
            tbCategoryBrand.setBrandId(brand.getId().intValue());
            tbCategoryBrand.setCategoryId(cids.get(i).intValue());
            System.out.println("查询测试数据----->:" + tbCategoryBrand.toString());
            this.tbCategoryBrandMapper.insertSelective(tbCategoryBrand);
        }
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 编辑商品
     * @param brand 品牌信息
     * @param cids 分类编号
     * @return ResponseEntity<Void>
     */
    @Override
    public ResponseEntity<Void> editBrand(Brand brand, List<Long> cids) {
        // 修改品牌信息
        brandMapper.updateByPrimaryKey(brand);
        // 修改品牌分类----->先删除该品牌的所有分类
        TbCategoryBrand tbCategoryBrand = new TbCategoryBrand();
        tbCategoryBrand.setBrandId(brand.getId().intValue());
        tbCategoryBrandMapper.delete(tbCategoryBrand);
        // 将edit之后的分类列表加入中间表
        for (int i = 0; i < cids.size(); i++) {
            tbCategoryBrand = new TbCategoryBrand();
            tbCategoryBrand.setBrandId(brand.getId().intValue());
            tbCategoryBrand.setCategoryId(cids.get(i).intValue());
            this.tbCategoryBrandMapper.insertSelective(tbCategoryBrand);
        }
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除品牌的相关信息
     * @param brandId 品牌id
     * @return ResponseEntity<Void
     */
    @Override
    public ResponseEntity<Void> deleteBrand(Long brandId) {
        // 补全Brand信息
        Brand brand = new Brand();
        brand.setId(brandId);
        // 删除tb_brand中的信息
        brandMapper.deleteByPrimaryKey(brand);
        // 补全TbCategoryBrand信息
        TbCategoryBrand tbCategoryBrand = new TbCategoryBrand();
        tbCategoryBrand.setBrandId(brandId.intValue());
        tbCategoryBrandMapper.delete(tbCategoryBrand);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据查询条件分页并排序查询品牌信息（使用的通用mapper+PageHelper实现分页查询）
     * @param key 模糊查询的条件
     * @param page 当前页数
     * @param rows 当前页的数据量
     * @param sortBy 需要排序的字段
     * @param desc 是否降序
     * @return PageResult<Brand>
     */
    @Override
    public PageResult<Brand> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        System.out.println("输出相应的参数----->:" + key + " " + page + " " + rows + " " + sortBy + " " + desc);
        // 初始化example对象
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        // 根据name模糊查询，或者根据首字母查询
        if (StringUtils.isNotBlank(key)) {
            criteria.andLike("name", "%" + key + "%").orEqualTo("letter", key);
        }

        // 添加排序条件
        if (StringUtils.isNotBlank(sortBy)) {
            example.setOrderByClause(sortBy + " " + (desc ? "desc" : "asc"));
        }
        // 从数据库查询数据
        List<Brand> brandList = this.brandMapper.selectByExample(example);
        System.out.println("从数据库中查询出来的数据----->:" + brandList.toString());

        // 添加分页条件
        PageHelper.startPage(page, rows);

        // 包装成pageInfo
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);

        System.out.println("csacsacasca----->:" + pageInfo.toString());
        // 包装成分页结果集返回
        return new PageResult<>((int)pageInfo.getTotal(),pageInfo.getPages(), pageInfo.getList());
    }

    /**
     * 根据查询条件分页并排序查询品牌信息（使用的Sql语句实现分页查询）
     * @param key 模糊查询的条件
     * @param page 当前页数
     * @param rows 当前页的数据量
     * @param sortBy 需要排序的字段
     * @param desc 是否降序
     * @return PageResult<Brand>
     */
    @Override
    public PageResult<Brand> queryBrandByPageSql(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        System.out.println("输出相应的参数----->:" + key + " " + page + " " + rows + " " + sortBy + " " + desc);
        // 调用mapper层中相应的方法----->查询分页的数据
        List<Brand> brands = brandMapper.queryBrandByPageSqls(key, page, rows, sortBy, desc,true);

        // 查询所有数据
        List<Brand> brandsAll = brandMapper.queryBrandByPageSqls(key, page, rows, sortBy, desc, false);

        // 包装成分页结果集返回
        return new PageResult<>(brandsAll.size(),(brandsAll.size()/brands.size()),brands);
    }

    /**
     * 查询该商品分类下的品牌
     * @param cid 商品分类的id
     * @return ResponseEntity<List<Brand>>
     */
    @Override
    public List<Brand> queryBrandByCid(Long cid) {
        return brandMapper.queryBrandByCid(cid);
    }

    @Override
    public Brand queryBrandById(Long id) {
        return this.brandMapper.selectByPrimaryKey(id);
    }
}
